package org.edu.modules.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
//import org.edu.modules.common.entity.*;
import org.edu.common.api.vo.Result;
import org.edu.modules.common.entity.*;
import org.edu.modules.common.mapper.VeBaseBanJiMapper;

import org.edu.modules.common.service.*;
import org.edu.modules.common.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VeBaseBanJiServiceImpl extends ServiceImpl<VeBaseBanJiMapper, VeBaseBanJi> implements IVeBaseBanJiService {
    @Autowired
    private VeBaseBanJiMapper veBaseBanJiMapper;

    @Autowired
    private IVeBaseGradeService iVeBaseGradeService;

    @Autowired
    private IVeBaseSpecialtyService iVeBaseSpecialtyService;

    @Autowired
    private IVeBaseCampusService veBaseCampusService;

    @Autowired
    private IVeBaseBuildService veBaseBuildService;

    @Autowired
    private IVeBaseBuildRoomService veBaseBuildRoomService;

   @Autowired
   private  IVeBaseTeacherService veBaseTeacherService;

   @Autowired
   private  IVeBaseStudentService veBaseStudentService;





    public List<VeBaseBanJi> getBanJiListBySpecId(Integer id, Integer bystatus) {
        List<VeBaseBanJi> list = this.veBaseBanJiMapper.getBanJiListBySpecId(id, bystatus);
        if (list.size() > 0) {
            for (VeBaseBanJi veBaseBanJi : list) {
                if (veBaseBanJi.getBysj() != null) {
                    veBaseBanJi.setBysjName(DateTimeUtil.timestampToDate(veBaseBanJi.getBysj().intValue()));
                } else {
                    veBaseBanJi.setBysjName("");
                }
            }
        }
        return list;
    }

    public List<VeBaseBanJi> queryBanJiListBySpecAndGradeId(Integer specId, Integer gradeId) {
        List<VeBaseBanJi> list = this.veBaseBanJiMapper.queryBanJiListBySpecAndGradeId(specId, gradeId);
        if (list.size() > 0) {
            for (VeBaseBanJi veBaseBanJi : list) {
                if (veBaseBanJi.getBysj() != null) {
                    veBaseBanJi.setBysjName(DateTimeUtil.timestampToDate(veBaseBanJi.getBysj().intValue()));
                } else {
                    veBaseBanJi.setBysjName("");
                }
            }
        }
        return list;
    }

    public List<VeBaseBanJi> getBanJiListByGradeId(Integer gradeId) {
        List<VeBaseBanJi> list = this.veBaseBanJiMapper.getBanJiListByGradeId(gradeId);
        if (list.size() > 0) {
            for (VeBaseBanJi veBaseBanJi : list) {
                if (veBaseBanJi.getBysj() != null) {
                    veBaseBanJi.setBysjName(DateTimeUtil.timestampToDate(veBaseBanJi.getBysj().intValue()));
                } else {
                    veBaseBanJi.setBysjName("");
                }
            }
        }
        return list;
    }

    public List<Map<String, Object>> getBanJiPageList(VeBaseBanJi veBaseBanJi) {
        List<Map<String, Object>> list = this.veBaseBanJiMapper.getBanJiPageList(veBaseBanJi);
        list = setRQName(list);
        return list;
    }

    public List<Map<String, Object>> queryBanJiPageListBySearch(String code, String bjmc) {
        List<Map<String, Object>> list = this.veBaseBanJiMapper.queryBanJiPageListBySearch(code, bjmc);

        list = setRQName(list);
        return list;
    }

    public VeBaseBanJi getBanJiByName(Integer id, String name) {
        VeBaseBanJi veBaseBanJi = this.veBaseBanJiMapper.getBanJiByName(id, name);
        if (veBaseBanJi != null) {
            if (veBaseBanJi.getBysj() != null) {
                veBaseBanJi.setBysjName(DateTimeUtil.timestampToDate(veBaseBanJi.getBysj().intValue()));
            } else {
                veBaseBanJi.setBysjName("");
            }
        }
        return veBaseBanJi;
    }

    public VeBaseBanJi getBanJiByCode(Integer id, String code) {
        VeBaseBanJi veBaseBanJi = this.veBaseBanJiMapper.getBanJiByCode(id, code);
        if (veBaseBanJi != null) {
            if (veBaseBanJi.getBysj() != null) {
                veBaseBanJi.setBysjName(DateTimeUtil.timestampToDate(veBaseBanJi.getBysj().intValue()));
            } else {
                veBaseBanJi.setBysjName("");
            }
        }
        return veBaseBanJi;
    }

    public int updateBanJiSchedule() {
        Long times = Long.valueOf(System.currentTimeMillis() / 1000L);
        return this.veBaseBanJiMapper.updateBanJiSchedule(times.intValue());
    }

    @Override
    public int importExcel(List<Map<String, Object>> mapList) {
        for (Map<String, Object> map : mapList) {
//            VeBaseBanJi dto = new VeBaseBanJi();
////                            dto.setStuId ((Integer) map.get("学生号"));
//            dto.setXzbdm((String) map.get("行政班代码"));
//            dto.setXzbmc((String) map.get("行政班名称"));
//            dto.setGradeId(27);//所属年级 默认24级
//            dto.setBzrUserId("001");//班主任，默认张老师
////            dto.setSpecId(118);//所属专业，默认计算机软件科学
//            dto.setSpecName((String) map.get("所属专业"));
//            dto.setCampusName((String) map.get("校区名称"));
//            dto.setCampusId(13);//默认德阳校区
//            dto.setJxlName((String) map.get("教学楼名称"));
//            dto.setRoomName((String) map.get("固定教室名称"));
//            this.save(dto);
//
//        System.out.println("mapList:" + mapList);

            VeBaseBanJi dto = new VeBaseBanJi();
//                            dto.setTerminalid ((String)map.get(0000));
            dto.setXzbdm(map.get("班级代码") + "");
            dto.setXzbmc(map.get("班级名称") + "");
            dto.setNjdm(map.get("所属年级代码") + "");
            dto.setNjmc(map.get("所属年级") + "");

            VeBaseGrade v = iVeBaseGradeService.getGradeByName(null, map.get("所属年级").toString());
            if (v != null) {
                dto.setGradeId(v.getId());
            } else {
                System.out.println("VeBaseGrade em");
                return 1;
            }
//                            dto.setGradeId (27);
            dto.setZydm(map.get("专业代码") + "");

            VeBaseSpecialty veBaseSpecialty = iVeBaseSpecialtyService.getSpecialtyByName(null,map.get("专业名称") + "");
            if (veBaseSpecialty != null  ) {
                dto.setSpecId(veBaseSpecialty.getId());
            } else {
                return 2;
            }
            VeBaseTeacher veBaseTeacher = new VeBaseTeacher();
            veBaseTeacher.setXm(map.get("所属班主任").toString());
            List<VeBaseTeacher> list1 = veBaseTeacherService.getTeacherListBySearch(veBaseTeacher);
            if (list1 != null & list1.size() > 0) {
                dto.setBzrUserId(list1.get(0).getUserId());
            } else {
                System.out.println("veBaseTeacher em");
                return 3;
            }
            List<Map<String, Object>> list2 = veBaseStudentService.getModelByName(map.get("班长").toString());
            if (list2 != null & list2.size() > 0) {
                dto.setBzUserId(list2.get(0).get("id").toString());
            } else {
                System.out.println("ModelByName em");
                return 4;
            }
//            dto.setZrs(map.get("专业名称")+"");
            int times = Integer.valueOf((int) DateTimeUtil.dateToTimestamp(map.get("毕业时间").toString()));
            dto.setBysj(times);
//                            dto.setBzruserid ((String)map.get(0000));

            dto.setJbny(map.get("建班年月").toString());
//            dto.setNansrs (Integer.parseInt(map.get("所属班主任")+""));
//            dto.setNvsrs (Integer.parseInt(map.get("女生人数")+""));
            dto.setZrs(Integer.parseInt(map.get("班级上限人数") + ""));
//                            dto.setBzuserid ((String)map.get(0000));
            dto.setBzrUserName(map.get("所属班主任") + "");
//                            dto.setDepid ((String)map.get(0000));
//                            dto.setCampusId (13);
//                            dto.setJzid ((String)map.get(0000));
//                            dto.setJsid ((String)map.get(0000));
            if (map.get("毕业时间") != null) {
//                dto.setBysj (Integer.parseInt(map.get("毕业时间")+""));
            }
            if (map.get("排序") != null) {
                dto.setListsort(Integer.parseInt(map.get("排序") + ""));
            }
            dto.setBystatus(0);
            dto.setStatus(1);

            List<Map<String, Object>> baselist= veBaseBuildService.queryJianzhuByName( map.get("固定教室名称").toString());
            if(baselist!=null&baselist.size()>0){
                dto.setJsid(Integer.valueOf(baselist.get(0).get("id").toString()));
                dto.setJzid(Integer.valueOf(baselist.get(0).get("jzId").toString()));
            }else {
                return 5;
            }
            dto.setRxnf(v.getRxnf().toString());
//            if (map.get("毕业班级状态，0=>未毕业，1=>毕业审核中， 2=>已毕业")!=null)
//            dto.setBystatus (Integer.parseInt(map.get("毕业班级状态，0=>未毕业，1=>毕业审核中， 2=>已毕业")+""));

            this.save(dto);

        }
        return 0;
    }
//        StringBuilder sb = new StringBuilder();
//        for (Map<String, Object> map : mapList) {
//            // 创建 ObjectMapper 实例
//            ObjectMapper objectMapper = new ObjectMapper();
//
////            try {
//                // 将 Map 转换为 User 对象
//                VeBaseBanJi veBaseBanJi = objectMapper.convertValue(map, VeBaseBanJi.class);
////                System.out.println("veBaseBanJi: " + veBaseBanJi);
////                if ((!"".equals(veBaseBanJi.getXzbdm())) && (veBaseBanJi.getXzbdm() != null) &&
////                        (!"".equals(veBaseBanJi.getXzbmc())) && (veBaseBanJi.getXzbmc() != null) &&
////                        (!"".equals(veBaseBanJi.getNjdm())) && (veBaseBanJi.getNjdm() != null) &&
////                        (!"".equals(veBaseBanJi.getSpecName())) && (veBaseBanJi.getSpecName() != null) &&
////                        (!"".equals(veBaseBanJi.getBzrUserId())) && (veBaseBanJi.getBzrUserId() != null) &&
////                        (!"".equals(veBaseBanJi.getCampusName())) && (veBaseBanJi.getCampusName() != null) &&
////                        (!"".equals(veBaseBanJi.getJxlName())) && (veBaseBanJi.getJxlName() != null) &&
////                        (!"".equals(veBaseBanJi.getRoomName())) && (veBaseBanJi.getRoomName() != null)) {
////                    VeBaseBanJi v1 = this.getBanJiByCode(null, veBaseBanJi.getXzbdm());
////
////                    VeBaseBanJi v2 = this.getBanJiByName(null, veBaseBanJi.getXzbmc());
////                    if ((v1 == null) && (v2 == null)) {
////                        String sname = veBaseBanJi.getNjdm() + "级";
////                        veBaseBanJi.setNjmc(sname);
////                        VeBaseGrade grade = iVeBaseGradeService.getGradeByName(null, sname);
////                        if (grade != null) {
////                            veBaseBanJi.setGradeId(grade.getId());
////                            veBaseBanJi.setNjdm(grade.getNjdm());
////                            veBaseBanJi.setRxnf(grade.getRxnf().toString());
////                        } else {
////                            veBaseBanJi.setGradeId(Integer.valueOf(0));
////                            veBaseBanJi.setNjdm("");
////                            veBaseBanJi.setRxnf("");
////                        }
////                        VeBaseSpecialty specialty = iVeBaseSpecialtyService.getSpecialtyByName(null, veBaseBanJi.getSpecName());
////                        if (specialty != null) {
////                            veBaseBanJi.setSpecId(specialty.getId());
////                            veBaseBanJi.setZydm(specialty.getZydm());
////                        } else {
////                            veBaseBanJi.setSpecId(Integer.valueOf(0));
////                            veBaseBanJi.setZydm("");
////                        }
////                        VeBaseCampus campus = this.veBaseCampusService.getCampusByName(null, veBaseBanJi.getCampusName());
////                        if (campus != null) {
////                            veBaseBanJi.setCampusId(campus.getId());
////                        }
////                        VeBaseBuild build = this.veBaseBuildService.getBuildByName(null, veBaseBanJi.getJxlName());
////                        if (build != null) {
////                            veBaseBanJi.setJzid(build.getBuildId());
////                        }
////                        VeBaseBuildRoom room = this.veBaseBuildRoomService.getBuildRoomByName(null, veBaseBanJi.getRoomName());
////                        if (room != null) {
////                            veBaseBanJi.setJsid(room.getId());
////                        }
////                        if ((!"".equals(veBaseBanJi.getBysjName())) && (veBaseBanJi.getBysjName() != null)) {
////                            Date date = HSSFDateUtil.getJavaDate(Double.parseDouble(veBaseBanJi.getBysjName()));
////                            Long l = Long.valueOf(date.getTime() / 1000L);
////                            veBaseBanJi.setBysj(Integer.valueOf(l.intValue()));
////                        }
//                        this.save(veBaseBanJi);

//                    } else {
//                        sb.append(" 行政班代码:" + veBaseBanJi.getXzbdm() + "或者行政班名称:" + veBaseBanJi.getXzbmc() + "已存在;");
//                    }
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }


//        }



    private List<Map<String, Object>> setRQName(List<Map<String, Object>> list) {
        if (list.size() > 0) {
            for (Map map : list) {
                if ((!"".equals(map.get("bysj"))) && (map.get("bysj") != null)) {
                    Long bysj = Long.valueOf(Long.parseLong(map.get("bysj").toString()));
                    map.put("bysjName", DateTimeUtil.timestampToDate(bysj.longValue()));
                } else {
                    map.put("bysjName", "");
                }
            }
        }
        return list;
    }
}

